<template>
  <div class="dashboard-container">
    <div class="dashboard-editor-container">
      <!--<yshop-info />-->
      <div class="divBox">
        <el-row :gutter="24" class="baseInfo">
          <el-col :xs="24" :sm="24" :lg="24" class="ivu-mb">
            <el-card>
              <div class="item-container">
                <div class="item">
                  <span class="name">床位总数</span>
                  <span class="num">40床</span>
                </div>
                <el-divider direction="vertical"></el-divider>
                <div class="item">
                  <span class="name">建设中床位</span>
                  <span class="num">19床</span>
                </div>
                <el-divider direction="vertical"></el-divider>
                <div class="item">
                  <span class="name">运营中床位</span>
                  <span class="num">12床</span>
                </div>
                <el-divider direction="vertical"></el-divider>
                <div class="item">
                  <span class="name">服务总人数</span>
                  <span class="num">1人</span>
                </div>
                <el-divider direction="vertical"></el-divider>
                <div class="item">
                  <span class="name">改造总床次</span>
                  <span class="num">21床</span>
                </div>
                <el-divider direction="vertical"></el-divider>
                <div class="item">
                  <span class="name">智能设备绑定数</span>
                  <span class="num">5件</span>
                </div>
                <el-divider direction="vertical"></el-divider>
                <div class="item">
                  <span class="name">事件处理</span>
                  <span class="num">245件</span>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="dashboard-console-grid ivu-mb">
          <el-col :xl="12" :lg="12" :md="24" :sm="24" :xs="24">
            <el-card :bordered="false" dis-hover class="left">
              <div slot="header">
                <div class="acea-row row-middle">
                  <span class="title">
                    <el-avatar
                      icon="el-icon-s-operation"
                      size="small"
                      style="
                        color: #1890ff;
                        background: #e6f7ff;
                        font-size: 13px;
                      "
                    />
                    <span class="ivu-pl-8">实时报警</span>
                  </span>
                </div>
              </div>
              <el-table
                :data="list"
                element-loading-text="Loading"
                border
                fit
                highlight-current-row
              >
                <el-table-column label="设备ID" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.id }}
                  </template>
                </el-table-column>
                <el-table-column label="时间" width="200" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.display_time }}
                  </template>
                </el-table-column>
                <el-table-column label="地址" width="110" align="center">
                  <template slot-scope="scope">
                    <span>{{ scope.row.place }}</span>
                  </template>
                </el-table-column>

                <el-table-column
                  class-name="status-col"
                  label="当前状态"
                  width="110"
                  align="center"
                >
                  <template slot-scope="scope">
                    <el-tag :type="scope.row.status | statusFilter">{{
                      scope.row.status | statusFilterLabel
                    }}</el-tag>
                  </template>
                </el-table-column>
              </el-table>
            </el-card>
          </el-col>
          <el-col :xl="12" :lg="12" :md="24" :sm="24" :xs="24">
            <el-card :bordered="false" dis-hover class="left">
              <div slot="header">
                <div class="acea-row row-middle">
                  <span class="title">
                    <el-avatar
                      icon="el-icon-s-operation"
                      size="small"
                      style="
                        color: #1890ff;
                        background: #e6f7ff;
                        font-size: 13px;
                      "
                    />
                    <span class="ivu-pl-8">服务工单</span>
                  </span>
                </div>
              </div>
              <el-table
                :data="list2"
                element-loading-text="Loading"
                border
                fit
                highlight-current-row
              >
                <el-table-column label="设备ID" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.id }}
                  </template>
                </el-table-column>
                <el-table-column label="时间" width="160" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.display_time }}
                  </template>
                </el-table-column>
                <el-table-column label="地址" width="110" align="center">
                  <template slot-scope="scope">
                    <span>{{ scope.row.place }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="报警内容" width="110" align="center">
                  <template slot-scope="scope">
                    <span>{{ scope.row.desc }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="告警值" min-width="110" align="center">
                  <template slot-scope="scope">
                    <span>{{ scope.row.num }}</span>
                  </template>
                </el-table-column>
              </el-table>
            </el-card>
          </el-col>
        </el-row>
      </div>
      <div class="divBox">
        <el-row :gutter="24" class="dashboard-console-grid ivu-mb">
          <el-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
            <el-card :bordered="false" dis-hover class="echart-card">
              <div slot="header">
                <div class="acea-row row-middle">
                  <span class="title">
                    <el-avatar
                      icon="el-icon-s-operation"
                      size="small"
                      style="
                        color: #1890ff;
                        background: #e6f7ff;
                        font-size: 13px;
                      "
                    />
                    <span class="ivu-pl-8">能力评估统计</span>
                  </span>
                </div>
              </div>
              <el-row :gutter="24">
                <el-col
                  :xl="8"
                  :lg="8"
                  :md="24"
                  :sm="24"
                  :xs="24"
                  class="ivu-mb mb10 dashboard-console-visit"
                >
                  <span class="ivu-pl-8">性别</span>
                  <div id="barChart1" style="width: 100%;height:220px;"></div>
                </el-col>
                <el-col
                  :xl="8"
                  :lg="8"
                  :md="24"
                  :sm="24"
                  :xs="24"
                  class="ivu-mb mb10 dashboard-console-visit"
                >
                  <span class="ivu-pl-8">改造情况</span>
                  <div id="barChart2" style="width: 100%;height:220px;"></div>
                </el-col>
                <el-col
                  :xl="8"
                  :lg="8"
                  :md="24"
                  :sm="24"
                  :xs="24"
                  class="ivu-mb mb10 dashboard-console-visit"
                >
                  <span class="ivu-pl-8">自理能力</span>
                  <div id="barChart3" style="width: 100%;height:220px;"></div>
                </el-col>
              </el-row>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <div class="divBox">
        <el-row :gutter="24" class="dashboard-console-grid">
          <el-col :xl="18" :lg="18" :md="24" :sm="24" :xs="24">
            <el-card :bordered="false" dis-hover class="left">
              <div slot="header">
                <div class="acea-row row-middle">
                  <span class="title">
                    <el-avatar
                      icon="el-icon-s-operation"
                      size="small"
                      style="
                        color: #1890ff;
                        background: #e6f7ff;
                        font-size: 13px;
                      "
                    />
                    <span class="ivu-pl-8">服务排名（TOP8）</span>
                  </span>
                </div>
              </div>
              <div id="pieChart" style="width: 100%;height:220px;"></div>
            </el-card>
          </el-col>
          <el-col :xl="6" :lg="6" :md="24" :sm="24" :xs="24">
            <el-card :bordered="false" dis-hover class="right">
              <div slot="header">
                <div class="acea-row row-middle">
                  <span class="title">
                    <el-avatar
                      icon="el-icon-message-solid"
                      size="small"
                      style="
                        color: #1890ff;
                        background: #e6f7ff;
                        font-size: 13px;
                      "
                    />
                    <span class="ivu-pl-8">最新消息通知</span>
                  </span>
                  <el-link class="ivu-pl-8" @click="messageAll">全部 ></el-link>
                </div>
              </div>
              <div v-if="massageList && massageList.length > 0" class="content">
                <div
                  v-for="(item, index) in massageList"
                  :key="index"
                  class="content-item"
                  @click="messageDetail(item)"
                >
                  <div class="row">
                    <div class="title">{{ item.content }}</div>
                    <!-- <div class="name">{{ item.id }}</div> -->
                  </div>
                  <div class="time">{{ item.sendTime }}</div>
                </div>
              </div>
              <div v-else>
                <img src="@/assets/images/empty.png" alt="" class="empty" />
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme

/**
 * 记录访问，只有页面刷新或者第一次加载才会记录
 */
// count().then((res) => { });

export default {
  name: "Dashboard",
  components: {},
  computed: {
    ...mapGetters(["name"])
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: "success",
        1: "gray",
        2: "danger",
        3: "warning"
      };
      return statusMap[status];
    },
    statusFilterLabel(status) {
      const statusMap = {
        0: "已处理",
        1: "",
        2: "报警中",
        3: "待启动"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      pageNum: 1,
      pageSize: 5,
      massageList: [],
      num1: 25,
      num2: 32,
      num3: 22,
      num4: 10,
      list: [
        {
          name: "张三",
          type: "基础类",
          author: "李响",
          display_time: "2023-12-24 16:10:10",
          id: "2104623",
          pageviews: 1165,
          status: 0,
          place: "拱墅区131号"
        },
        {
          name: "李四",
          type: "完善类",
          author: "李响",
          display_time: "2023-12-24 16:10:10",
          id: "2104622",
          pageviews: 1165,
          status: 2,
          place: "拱墅区131号"
        },
        {
          name: "王五",
          type: "提升类",
          author: "李响",
          display_time: "2023-12-24 16:10:10",
          id: "2104628",
          pageviews: 1165,
          status: 2,
          place: "拱墅区131号"
        }
      ],
      list2: [
        {
          name: "张三",
          type: "基础类",
          author: "李响",
          display_time: "2023-12-24 16:10:10",
          id: "2104623",
          pageviews: 1165,
          status: 0,
          place: "拱墅区131号",
          desc: "跌倒报警",
          num: ""
        },
        {
          name: "李四",
          type: "完善类",
          author: "李响",
          display_time: "2023-12-24 16:10:10",
          id: "2104622",
          pageviews: 1165,
          status: 2,
          place: "拱墅区131号",
          desc: "雷达波报警",
          num: "心率 100次/分钟"
        },
        {
          name: "王五",
          type: "提升类",
          author: "李响",
          display_time: "2023-12-24 16:10:10",
          id: "2104628",
          pageviews: 1165,
          status: 2,
          place: "拱墅区131号",
          desc: "烟感报警",
          num: "0.60db/m"
        }
      ],
      type1: "week",
      type2: "week",
      lineChart1: null,
      lineChart2: null,
      barChart1: null,
      barChart2: null,
      barChart3: null,
      pieChart: null
    };
  },
  created() {
    this.$nextTick(() => {
      // this.getData()
      this.initChart();
    });
  },
  mounted() {
    this.initChart();
    this.__resizeHandler = () => {
      setTimeout(() => {
        if (this.lineChart1) {
          this.lineChart1.resize();
        }
        if (this.lineChart2) {
          this.lineChart2.resize();
        }
        if (this.barChart1) {
          this.barChart1.resize();
        }
        if (this.barChart2) {
          this.barChart2.resize();
        }
        if (this.barChart3) {
          this.barChart3.resize();
        }
        if (this.pieChart) {
          this.pieChart.resize();
        }
      }, 100);
    };
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    // if (!this.chart) {
    //   return;
    // }
    if (this.lineChart1) {
      window.removeEventListener("resize", this.__resizeHandler);
      this.lineChart1.dispose();
      this.lineChart1 = null;
    }
    if (this.lineChart2) {
      window.removeEventListener("resize", this.__resizeHandler);
      this.lineChart2.dispose();
      this.lineChart2 = null;
    }
    if (this.barChart1) {
      window.removeEventListener("resize", this.__resizeHandler);
      this.barChart1.dispose();
      this.barChart1 = null;
    }
    if (this.barChart2) {
      window.removeEventListener("resize", this.__resizeHandler);
      this.barChart2.dispose();
      this.barChart2 = null;
    }
    if (this.barChart3) {
      window.removeEventListener("resize", this.__resizeHandler);
      this.barChart3.dispose();
      this.barChart3 = null;
    }
    if (this.pieChart) {
      window.removeEventListener("resize", this.__resizeHandler);
      this.pieChart.dispose();
      this.pieChart = null;
    }
  },
  methods: {
    initChart() {
      // this.chartLine1();
      // this.chartLine2();
      this.chartBar1();
      this.chartBar2();
      this.chartBar3();
      this.chartPie();
    },
    chartLine1() {
      var chartDom = document.getElementById("lineChart1");
      this.lineChart1 = echarts.init(chartDom);
      var option;

      option = {
        grid: {
          top: "10px",
          left: "40px",
          right: "15px",
          bottom: "20px"
        },
        xAxis: {
          type: "category",
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
        },
        yAxis: {
          type: "value"
        },
        series: [
          {
            data: [150, 230, 224, 218, 135, 147, 260],
            type: "line"
          }
        ]
      };

      option && this.lineChart1.setOption(option);
    },
    chartLine2() {
      var chartDom = document.getElementById("lineChart2");
      this.lineChart2 = echarts.init(chartDom);
      var option;

      option = {
        grid: {
          top: "10px",
          left: "40px",
          right: "15px",
          bottom: "20px"
        },
        xAxis: {
          type: "category",
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
        },
        yAxis: {
          type: "value"
        },
        series: [
          {
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: "line",
            smooth: true
          }
        ]
      };

      option && this.lineChart2.setOption(option);
    },
    chartBar1() {
      var chartDom = document.getElementById("barChart1");
      this.barChart1 = echarts.init(chartDom);
      var option;

      option = {
        tooltip: {
          trigger: "item"
        },
        legend: {
          orient: "horizontal",
          bottom: "bottom"
        },
        series: [
          {
            name: "",
            type: "pie",
            radius: "50%",
            data: [
              { value: 84, name: "男" },
              { value: 46, name: "女" }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      };

      option && this.barChart1.setOption(option);
    },
    chartBar2() {
      var chartDom = document.getElementById("barChart2");
      this.barChart2 = echarts.init(chartDom);
      var option;

      option = {
        tooltip: {
          trigger: "item"
        },
        legend: {
          orient: "horizontal",
          bottom: "bottom"
        },
        series: [
          {
            name: "项目数",
            type: "pie",
            left: 0,
            radius: "50%",
            center: ["50%", "50%"],
            data: [
              { value: 4, name: "改造中" },
              { value: 95, name: "待验收" },
              { value: 119, name: "已完成" }
            ],
            avoidLabelOverlap: true,
            // labelLine: {
            //   length: 12,
            //   length2: 6
            // },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      };

      option && this.barChart2.setOption(option);
    },
    chartBar3() {
      var chartDom = document.getElementById("barChart3");
      this.barChart3 = echarts.init(chartDom);
      var option;

      option = {
        tooltip: {
          trigger: "item"
        },
        legend: {
          orient: "horizontal",
          bottom: "bottom"
        },
        series: [
          {
            name: "人数",
            type: "pie",
            radius: "50%",
            data: [
              { value: 349, name: "能力完好" },
              { value: 280, name: "半失能" },
              { value: 69, name: "重度失能" }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      };

      option && this.barChart3.setOption(option);
    },
    chartPie() {
      var chartDom = document.getElementById("pieChart");
      this.pieChart = echarts.init(chartDom);
      var option;

      option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow"
          }
        },
        legend: {},
        grid: {
          top: "2%",
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true
        },
        xAxis: {
          type: "value",
          boundaryGap: [0, 0.01]
        },
        yAxis: {
          type: "category",
          data: [
            "卫生护理",
            "心理疏导",
            "紧急救援",
            "清洁消毒",
            "康复训练",
            "用药护理",
            "头面部清洁、护理",
            "维持生命体征"
          ]
        },
        series: [
          {
            name: "",
            type: "bar",
            data: [299, 339, 399, 439, 500, 523, 699, 889]
          }
        ]
      };

      option && this.pieChart.setOption(option);
    },
    messageAll() {
      this.$router.push({ name: "message" });
    },
    messageDetail(item) {
      messageRead(item.id).then(res => {
        if (item.link) {
          const sysUrl =
            item.link +
            "&name=" +
            encodeURIComponent(localStorage.getItem("redirectName")) +
            "&pwd=" +
            encodeURIComponent(localStorage.getItem("redirectPwd"));
          window.open(sysUrl);
        }
      });
    },
    getData() {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
      messageSearch(params).then(res => {
        this.massageList = res.item.records;
        console.log(this.massageList);
      });
    },
    toMylist() {
      this.$router.push({ name: "Task" });
    },
    toHandle() {
      this.$router.push({ name: "Issue" });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dashboard-editor-container {
  padding: 18px 22px 22px 22px;
  // background-color: rgb(240, 242, 245);

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

.acea-row {
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .title {
    display: flex;
    align-items: center;
  }

  ::v-deep.el-avatar--small {
    width: 22px;
    height: 22px;
    line-height: 22px;
  }
}

.checkTime {
  ::v-deep.el-radio__input {
    display: none;
  }
}

.ivu-pl-8 {
  margin-left: 8px;
  font-size: 14px;
}

.divBox {
  .dashboard-console-grid {
    display: flex;

    .left {
      height: 100%;
    }

    .right {
      height: 100%;
    }

    .echart-card {
      ::v-deep .el-card__body {
        padding: 10px;
      }
    }

    .empty {
      width: 100%;
      height: 100%;
    }
  }
}

.dashboard-console-visit {
  ::v-deep.el-card__header {
    padding: 14px 20px !important;
  }

  ul {
    li {
      list-style-type: none;
      margin-top: 12px;
    }
  }
}

.ivu-mb {
  margin-bottom: 10px;
}

.content {
  // display: flex;

  .content-item {
    margin-bottom: 15px;
    cursor: pointer;

    .row {
      display: flex;
      justify-content: space-between;
      margin-bottom: 5px;

      .title {
        font-weight: bold;
        text-align: left;
      }
    }

    .time {
      color: #989898d7;
      font-size: 14px !important;
    }
  }

  .content-number {
    font-size: 28px;
    font-weight: 500;
  }

  .content-bottom {
    font-size: 20px;
  }
}

.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
    border-color: rgba(0, 0, 0, 0.05);

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3;
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width: 550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}

.baseInfo {
  ::v-deep .el-card__header {
    padding: 15px 20px !important;
    font-size: 18px;
  }
}

.divBox {
  .baseInfo {
    display: flex;
    flex-flow: row wrap;

    .el-card {
      height: 100%;
    }

    .ivu-mb {
      margin-bottom: 10px;
    }
  }
}

// .ivu-mb {
//   margin-bottom: 10px;
//   height: 100%;
// }

.up,
.el-icon-caret-top {
  color: #f5222d;
  font-size: 12px;
  opacity: 1 !important;
}

.down,
.el-icon-caret-bottom {
  color: #39c15b;
  font-size: 12px;
  /*opacity: 100% !important;*/
}

.content {
  &-number {
    font-size: 30px;
    cursor: pointer;
  }

  &-time {
    font-size: 14px;
    /*color: #8C8C8C;*/
  }

  &-total {
    font-size: 18px;
  }
}

.spBlock {
  display: block;
}
.item-container {
  display: flex;
  align-items: center;
  .el-divider {
    height: 80px;
  }
  .item {
    width: 20%;
    min-height: 100px;
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: center;

    .name {
      margin: 10px 0;
    }

    .num {
      font-size: 24px;
    }
  }
}
</style>
